package com.android.home.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import com.android.common.base.BaseFragment;
import com.android.common.constant.Constant;
import com.android.common.db.DBConfig;
import com.android.common.result.contract.CropImage;
import com.android.common.result.contract.PickPicture;
import com.android.common.result.contract.TakeCropPicture;
import com.android.common.utils.FileUtil;
import com.android.common.utils.GlideUtils;
import com.android.home.R;
import com.android.home.mvp.present.home.HomePresent;
import java.io.File;

public class PersonalInfoFragment extends BaseFragment {

    private static PersonalInfoFragment instance;

    private ImageView mIvAvatar;

    private Uri mCaptureUri;

    private Uri mCropUri;

    protected ActivityResultLauncher mTakePictureLauncher;

    protected ActivityResultLauncher mCropPictureLauncher;

    protected ActivityResultLauncher mPickPictureLauncher;

    protected TakeCropPicture mTakeCropPicture;

    private HomePresent present;

    public static PersonalInfoFragment getInstance() {
        if (null == instance) {
            instance = new PersonalInfoFragment();
        }
        return instance;
    }

    public PersonalInfoFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //拍照
        mTakePictureLauncher = registerForActivityResult(new ActivityResultContracts.TakePicture(), result -> {
            if (result){
                mCropPictureLauncher.launch(initCropImage());
            }else {
                Toast.makeText(getContext(),R.string.take_picture_fail,Toast.LENGTH_SHORT);
            }
        });
        //裁剪
        mCropPictureLauncher = registerForActivityResult(initTakeCropPicture(), result -> {
            if (null != result)
                GlideUtils.with().displayImage(result.toString(),mIvAvatar);
        });

        //相册选照片
        mPickPictureLauncher = registerForActivityResult(new PickPicture(), result -> {
            if (null != result)
                GlideUtils.with().displayImage(result.toString(),mIvAvatar);
        });

    }

    @Override
    public int onSetRootViewId() {
        return R.layout.activity_home_third;
    }

    @Override
    protected void onFindView() {
        super.onFindView();
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        mIvAvatar = mFlRootContent.findViewById(R.id.iv_avatar);
        present = new HomePresent(this);
    }

    @Override
    protected void onInitEvent() {
        super.onInitEvent();
        mIvAvatar.setOnClickListener(view -> {

        });
    }

    @Override
    protected boolean onNeedBack() {
        return false;
    }

    @Override
    protected String onSetTitleText() {
        return getString(R.string.my);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mTakePictureLauncher){
            mTakePictureLauncher.unregister();
        }

        if (null != mCropPictureLauncher){
            mCropPictureLauncher.unregister();
        }

        if (null != mPickPictureLauncher){
            mPickPictureLauncher.unregister();
        }
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        present.getMyInfoList();
    }

    private CropImage initCropImage(){
        CropImage.Builder builder = new CropImage.Builder();
        builder.setAspectX(Constant.CROP_ASPECT_X);
        builder.setAspectY(Constant.CROP_ASPECT_Y);
        builder.setWidth(Constant.CROP_OUTPUT_X);
        builder.setHeight(Constant.CROP_OUTPUT_Y);
        builder.setInPutUri(mCaptureUri);
        builder.setOutPutUri(mCropUri);
        return builder.build();
    }

    private Uri initCaptureUri() {
        if (null == mCaptureUri){
            File file = new File(FileUtil.getCapturePath(getContext()), FileUtil.getFileNameByTime());
            mCaptureUri = FileProvider.getUriForFile(getContext(), DBConfig.getAuthUrl(getContext()), file);
        }
        return mCaptureUri;
    }

    private Uri initCropUri() {
        if (null == mCropUri){
            File file = new File(FileUtil.getCropPath(), FileUtil.getFileNameByTime());
            mCropUri = Uri.fromFile(file);
        }
        return mCropUri;
    }

    private TakeCropPicture initTakeCropPicture() {
        if (null == mTakeCropPicture){
            mTakeCropPicture = new TakeCropPicture();
            mTakeCropPicture.setOutPutUri(initCropUri());
        }
        return mTakeCropPicture;
    }

}
